package com.yellowpage.alarm.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.alarm.data.AlarmInfoUser;
import com.yellowpage.alarm.service.AlarmInfoUserService;
import com.yellowpage.ca.service.DictionaryService;

public class AlarmInfoUserAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<AlarmInfoUser> alarmInfoUserList = new ArrayList<AlarmInfoUser>();
	private List<Map> resultList;
	private List<Map> userRelatedList;
	private List<Map> userNotRelatedList;

	private AlarmInfoUser alarmInfoUser;
	private AlarmInfoUserService alarmInfoUserService;
	private DictionaryService dictionaryService;

	private String alarmTypeName;

	private String selectedId;
	private String message;

	private String[] relatedUsers;
	private String[] unrelatedUsers;

	public void prepare() {
		super.prepare();
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();

		resultList = alarmInfoUserService.queryAlarmInfoUserByPage(queryMap,
				pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toEdit() {
		userRelatedList = alarmInfoUserService.getUserRelated(selectedId, true);
		userNotRelatedList = alarmInfoUserService.getUserRelated(selectedId,
				false);
		alarmTypeName = dictionaryService.getByDictValueAndDictType(selectedId,
				"ALARM_TYPE").getDictName();
		return RESULT_EDIT;
	}

	public String update() {
		List<Long> ids = new ArrayList<Long>();
		if (relatedUsers != null)
			for (String userId : relatedUsers) {
				ids.add(Long.valueOf(userId));
			}
		try {
			alarmInfoUserService.updateAlarmInfoUsers(selectedId, ids);
			this.addActionMessage("保存告警处理人关联成功!");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			this.addActionError("** 保存告警处理人关联失败!");
		}

		return toEdit();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setAlarmInfoUser(AlarmInfoUser alarmInfoUser) {
		this.alarmInfoUser = alarmInfoUser;
	}

	public AlarmInfoUser getAlarmInfoUser() {
		return alarmInfoUser;
	}

	public void setAlarmInfoUserService(
			AlarmInfoUserService alarmInfoUserService) {
		this.alarmInfoUserService = alarmInfoUserService;
	}

	public List<AlarmInfoUser> getAlarmInfoUserList() {
		return alarmInfoUserList;
	}

	public void setAlarmInfoUserList(List<AlarmInfoUser> alarmInfoUserList) {
		this.alarmInfoUserList = alarmInfoUserList;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public List<Map> getUserRelatedList() {
		return userRelatedList;
	}

	public List<Map> getUserNotRelatedList() {
		return userNotRelatedList;
	}

	public void setRelatedUsers(String[] relatedUsers) {
		this.relatedUsers = relatedUsers;
	}

	public void setUnrelatedUsers(String[] unrelatedUsers) {
		this.unrelatedUsers = unrelatedUsers;
	}

	public String getAlarmTypeName() {
		return alarmTypeName;
	}

	public void setAlarmTypeName(String alarmTypeName) {
		this.alarmTypeName = alarmTypeName;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String getSelectedId() {
		return selectedId;
	}

}
